
package com.csun.cmny.provider.web.rpc;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dao.OmcNursingRoomBindMapper;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.*;
import com.csun.cmny.provider.model.dto.cycle.CycleBillDto;
import com.csun.cmny.provider.model.vo.CacOldManVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.OldmanRpcVo;
import com.csun.cmny.provider.model.vo.PointCardAccountVO;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupOldmanVo;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupRoomVo;
import com.csun.cmny.provider.model.vo.nursing.NursingGroupUserVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.task.AnalysisQuartz;
import com.csun.cmny.provider.utils.CronUtil;
import com.csun.cmny.util.DateUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class OmcOldmanFeignClient extends BaseController implements OmcOldmanFeignApi {

    @Autowired
    private OldmanCheckInService oldmanCheckInService;
    @Autowired
    private OldmanService oldmanService;
    @Resource
    private OmcOldManSupplementService omcOldManSupplementService;
    @Resource
    private OmcNursingUserBindService omcNursingUserBindService;
    @Resource
    private OmcNursingRoomBindMapper omcNursingRoomBindMapper;
    @Resource
    private AnalysisQuartz analysisQuartz;
    @Resource
    private OmcPaymentCycleService omcPaymentCycleService;
    @Resource
    private OmcOldManBillService omcOldManBillService;
    @Resource
    private OmcShiftLogService omcShiftLogService;
    @Resource
    private RoomService roomService;
    @Resource
    private OmcPointCardAccountService omcPointCardAccountService;
    @Resource
    private OmcSpecialistService specialistService;
    @Resource
    private OmcServiceOrderService serviceOrderService;

    @Override
    @ApiOperation(httpMethod = "POST", value = "根据机构id查询所有已入院老人")
    public Wrapper getCheckinByHomeId(Integer homeId) {
        if (homeId == null){
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"机构id不能为空",null);
        }
        List<OldmanRpcVo> OldmanRpcVoList  = oldmanCheckInService.getCheckinByHomeId(homeId);
        return WrapMapper.ok(OldmanRpcVoList);
    }

    @Override
    @ApiOperation(httpMethod = "POST", value = "根据房间id查询入住老人id")
    public Wrapper selectOldByRoomId( Long roomId) {
        if (roomId == null){
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"房间id不能为空",null);
        }
        Long aLong = roomService.selectOldByRoomId(roomId);
        System.out.println("查到的room是"+aLong);
        return WrapMapper.ok(aLong);
    }

    @Override
    public Wrapper<List<PointCardAccountVO>> listPointCardAccount(PointCardAccountQueryDto dto) {
        List<PointCardAccountVO> list =  omcPointCardAccountService.listPointCardAccount(dto);
        return WrapMapper.ok(list);
    }

    @Override
    public Wrapper<Integer> addPointCardAccount(@RequestBody PointCardAccountDto dto) {

        int i = omcPointCardAccountService.addPointCardAccount(dto);

        return WrapMapper.ok(i);
    }

    @Override
    public Wrapper<Integer> deletePointCardAccount(Long deviceId) {
        int i = omcPointCardAccountService.deleteByKey(deviceId);
        return WrapMapper.ok(i);
    }

    @Override
    public Wrapper<List<Long>> selectStaffIdBySpecialist() {
        List<Long>staffIds=new ArrayList<>();
        List<OmcSpecialist> omcSpecialists = specialistService.selectAll();
        for (OmcSpecialist omcSpecialist : omcSpecialists) {
            staffIds.add(omcSpecialist.getStaffId());
        }
        return WrapMapper.ok(staffIds);
    }

    @Override
    public Wrapper<Integer> selectHaveUnfinishedOrderNumbers(Long userId) {

        Integer count=serviceOrderService.selectHaveUnfinishedOrderNumbers(userId);

        return WrapMapper.ok(count);
    }

    @Override
    public Wrapper<Integer> isSpecialist(Long id) {
        OmcSpecialist specialist = new OmcSpecialist();
        specialist.setStaffId(id);
        int count = specialistService.selectCount(specialist);

        return WrapMapper.ok(count);
    }

    @Override
    @ApiOperation(httpMethod = "POST", value = "判断老人是否属于改机构")
    public Wrapper checkOldmanbelong(OldmanRpcDto oldmanRpcDto) {
        if (oldmanRpcDto.getOldmanId() ==null){
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"老人id不能为空",null);
        }
        if (oldmanRpcDto.getOldmanId()==null){
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"机构id不能为空",null);
        }
        Oldman oldman =  oldmanService.checkOldmanbelong(oldmanRpcDto);

        OldmanRpcVo oldmanRpcVo = new OldmanRpcVo();
        if (oldman == null){
            return WrapMapper.error();
        }

        BeanUtils.copyProperties(oldman, oldmanRpcVo);

        return WrapMapper.ok(oldmanRpcVo);
    }

    @Override
    public Wrapper<OldmanBedRoomBuildingVo> queryOldmanBedRoomBuildingByOldmanId(Integer oldmanId) {

        OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = oldmanService.queryOldmanBedRoomBuildingByOldmanId(oldmanId);

        return WrapMapper.ok(oldmanBedRoomBuildingVo);
    }

    @Override
    public Wrapper<OldmanBedRoomBuildingVo> queryOldmanBedRoomBuildingByBedId(Integer bedId) {

        OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = oldmanService.queryOldmanBedRoomBuildingByBedId(bedId);

        return WrapMapper.ok(oldmanBedRoomBuildingVo);
    }

    @Override
    @ApiOperation(httpMethod = "POST", value = "根据姓名模糊查询老人信息")
    public Wrapper<List<CacOldManVo>> showCacOldMan(@RequestBody OmcOldManDto omcOldManDto) {
        List<CacOldManVo> list = new ArrayList<>();
        list = oldmanService.showCacOldMan(omcOldManDto);
        return WrapMapper.ok(list);
    }

    /**
     * query oldman special case by oldman id
     *
     * @param oldmanId the oldman id
     *
     * @return the oldman special case
     */
    @Override
    public Wrapper<String> queryOldmanSpecialCaseByOldmanId(Integer oldmanId) {

        String specialCase = omcOldManSupplementService.queryOldmanSpecialCaseByOldmanId(oldmanId);
        if (specialCase == null) {
            specialCase = new String();
        }

        return WrapMapper.ok(specialCase);
    }

    /**
     * query nursing group user by user id
     *
     * @param userId the user id
     *
     * @return the nursing group user vo
     */
    @Override
    public Wrapper<NursingGroupUserVo> queryNursingGroupUserByUserId(Long userId) {

        NursingGroupUserVo nursingGroupUserVo = new NursingGroupUserVo();

        OmcNursingUserBind omcNursingUserBind = new OmcNursingUserBind();
        omcNursingUserBind.setUserId(userId);
        omcNursingUserBind = omcNursingUserBindService.selectOne(omcNursingUserBind);
        if (omcNursingUserBind != null) {
            nursingGroupUserVo.setUserId(userId);
            nursingGroupUserVo.setNursingGroupId(omcNursingUserBind.getNursingGroupId());
        }

        return WrapMapper.ok(nursingGroupUserVo);
    }

    /**
     * query nursing group room by room id
     *
     * @param roomId the room id
     *
     * @return the nursing group room vo
     */
    @Override
    public Wrapper<NursingGroupRoomVo> queryNursingGroupRoomByRoomId(Integer roomId) {

        NursingGroupRoomVo nursingGroupRoomVo = new NursingGroupRoomVo();

        OmcNursingRoomBind omcNursingRoomBind = new OmcNursingRoomBind();
        omcNursingRoomBind.setRoomId((long) roomId);
        omcNursingRoomBind = omcNursingRoomBindMapper.selectOne(omcNursingRoomBind);
        if (omcNursingRoomBind != null) {
            nursingGroupRoomVo.setRoomId(roomId);
            nursingGroupRoomVo.setNursingGroupId(omcNursingRoomBind.getNursingGroupId());
        }

        return WrapMapper.ok(nursingGroupRoomVo);
    }

    /**
     * query nursing group oldman by oldman id
     *
     * @param oldmanId the oldman id
     *
     * @return the nursing grouo oldman vo
     */
    @Override
    public Wrapper<NursingGroupOldmanVo> queryNursingGroupOldmanByOldmanId(Integer oldmanId) {

        NursingGroupOldmanVo  nursingGroupOldmanVo = omcNursingRoomBindMapper
                .queryNursingGroupOldmanByOldmanId(oldmanId);

        if (nursingGroupOldmanVo == null) {
            nursingGroupOldmanVo = new NursingGroupOldmanVo();
        }

        return WrapMapper.ok(nursingGroupOldmanVo);
    }

    @Override
    public Wrapper<Integer> updateOldmanByEvaluate(@RequestBody CacOldmanDto cacOldmanDto) {
        int ret = oldmanService.updateOldmanByEvaluate(cacOldmanDto);
        return WrapMapper.ok(ret);
    }

    public Wrapper oldmanAnalysisCalc() {

        analysisQuartz.calc();

        return WrapMapper.ok();
    }

    @Test
    public void main() {

        Date date = DateUtil.strToDate("2019-12-05 00:00:00");
        // List<Date> dateList = CronUtil.getNextExecTimeOfMonthWithDate("00 00 00 16 1-12 ? *", date);
        List<Date> dateList = CronUtil.getNextExecTimeOfMonthWithDate("00 00 00 05 1-12 ? *", date);

        if (date.equals(dateList.get(0))) {
            System.out.println("is equal");
        } else {
            System.out.println("not equal");
        }

        for (Date d : dateList) {

            System.out.println("date list = " + DateUtil.dateToStr(d));
        }

    }

    @Override
    public Wrapper batchCreateBill(@RequestParam("date") String str) {

        log.info("batchCreateBill ==> date = {}", str);

        Date date = DateUtil.strToDate(str);

        // 1、查询账单配置
        List<OmcPaymentCycle> omcPaymentCycleList = omcPaymentCycleService.selectAll();

        try {
            // 2、计算需要生成账单的机构
            for (OmcPaymentCycle config : omcPaymentCycleList) {
                List<Date> dateList = CronUtil.getNextExecTimeOfMonthWithDate(config.getPaymentRecordGenerateConfig(), date);
                if (date.equals(dateList.get(0))) {
                    CycleBillDto cycleBillDto = new CycleBillDto();
                    cycleBillDto.setCreateTime(dateList.get(0));
                    cycleBillDto.setGroupId(config.getGroupId());

                    omcOldManBillService.createCycleBillWithGroupId(cycleBillDto);
                }
            }
        } catch (Exception e) {
            log.error("批量账单生成错误");
            e.printStackTrace();
        }

        return WrapMapper.ok();
    }

    @Override
    public Wrapper<Integer> syncAlertMsgToShiftLog(@RequestBody DeviceAlertDto dto) {

        log.info("远程调用 ==> syncAlertMsgToShiftLog");

        int ret =  omcShiftLogService.syncAlertMsgToShiftLog(dto);
        return WrapMapper.ok(ret);
    }

}

